
class DrillHole {
    constructor(options) {
        this.viewer = options.viewer;
        this.holeRadiu = options.holeRadiu || 20;
        this.holeLevel = options.holeLevel;

        // this.init()
        // this.viewer.camera.flyTo({
        //     destination: Cesium.Cartesian3.fromDegrees(110.238221, 39.034392, 4000),
        //     orientation: {
        //         heading: Cesium.Math.toRadians(10),
        //         pitch: Cesium.Math.toRadians(-90),
        //         roll: Cesium.Math.toRadians(6),
        //     },
        // });
    }

    compileData(target) {
        // 处理数据
        const holeList = {};
        this.holeLevel.forEach(item => {
            let textureMaterial = null
            if (item.img) {
                textureMaterial = new Cesium.ImageMaterialProperty({
                    image: item.img,
                    repeat: new Cesium.Cartesian2(16.0, 16.0)
                })
            } else {
                const { r, g, b, a } = $3d._factory._randomRgbaColor();
                textureMaterial = new Cesium.Color(r, g, b, a);
            }
            holeList[item.level] = {
                id: $3d._factory._randomString(),
                Level: item.level,
                radius: 4.0,
                material: textureMaterial,
                holeList: []
            }
        })
        const level = this.holeLevel.length;

        for (let i = 0; i < target.length; i++) {
            if (i % 20 !== 0) { continue }
            const feature = target[i];
            if (feature.geometry.type === 'Point') {
                let height = 0.0;
                for (let j = level - 1; j >= 0; j--) {
                    const key = this.holeLevel[j].level
                    let h = parseInt((feature.properties[key]) / this.holeRadiu);
                    holeList[key].holeList.push({ id: $3d._factory._randomString(), coordinates: feature.geometry.coordinates.concat([height]), length: h });
                    height += h;
                }
            }
        }
        return holeList;
    }
    initHole(target) {

        const holeList = this.compileData(target)
        Object.keys(holeList).forEach(key => {
            if (holeList[key].holeList.length) {
                holeList[key].holeList
                for (let i = 0; i < holeList[key].holeList.length; i++) {
                    const properties = holeList[key].holeList[i]
                    this.viewer.entities.add({
                        id: properties.id,
                        position: Cesium.Cartesian3.fromDegrees(properties.coordinates[0], properties.coordinates[1], properties.coordinates[2]),
                        cylinder: {
                            length: properties.length,
                            topRadius: holeList[key].radius,
                            bottomRadius: holeList[key].radius,
                            heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND, // CLAMP_TO_GROUND, // RELATIVE_TO_GROUND 
                            material: holeList[key].material || Cesium.Color.BLUE
                        }
                    });
                }
            }
        })
    }
}

export default DrillHole